--
-- Author: shunguo.chen
-- Date: 2016-06-21 16:49:56
--
-- IMMsgRender

local IMVoiceBtn = import(".IMVoiceBtn")
local IMPriRender = import(".IMPriRender")

local ClassRef = app.mvc.createRenderClass(...)

-- data = IMMessage
function ClassRef.create(node)
	local sprite = ClassRef.extend(node)

	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._IMPriRender = IMPriRender.create(self._ui.head)
	self._IMPriRender:setHasSelected(false)
	self._IMPriRender:onClick(function(sender)
			if self._data and not self._data:isFromSystem() and not self._data.isOutgoingMsg then
				local size = sender:getContentSize()
				local pos = sender:convertToWorldSpace(cc.p(size.width*7/8, size.height/2))
				
				executeFunc(self._onClickLink, "p|sender", self._data, pos)
			end
		end)

	self._ui.Text:setVisible(false)

	self._bg = self._ui.Panel_message
	self._initSize = self:getContentSize()
	self._bgSize = self._bg:getContentSize()
	
	local bg_x = self._bg:getPositionX()
	self._maxWid = self._initSize.width - bg_x - 10

	self._colorBg = self._bg:getChildByName("bg")
	display.setCascadeColorEnabled(self._colorBg, true)
	display.setCascadeOpacityEnabled(self._colorBg, true)

	-- dump(self._initSize, "initSize")
	-- dump(self._bgSize, "bgSize")
	-- dump(self._maxWid, "maxWid")

	self._textRT = nil

	self._voiceBtn = nil

	-- self:refresh()
end

local defaultColor = cc.c3b(40, 40, 40)
local outgoingColor = cc.c3b(73, 93, 113)

function ClassRef:refresh()
	if self._data then
		self._ui.Text_channel:setString("["..self._data.channelSName.."]")
		self._ui.Text_channel:setColor(self._data:getChannelColor())
		self._ui.Text_name:setString(self._data:getSenderName())

		local color
		if self._data:isFromSystem() then
			-- color = cc.c3b(255, 253, 56)
			color = defaultColor
		else
			color = self._data.isOutgoingMsg and outgoingColor or defaultColor
		end
		self._colorBg:setColor(color)

		if self._data:isFromSystem() then
			self._IMPriRender:setData()
		else
			self._IMPriRender:setData(self._data:getSenderInfo())
		end
		self:setContent(self._data)
	end
end

--msg = IMMessage
function ClassRef:setContent(msg)
	if self._textRT ~= nil then
		self._textRT:removeFromParent()
		self._textRT = nil
		self._voiceBtn = nil
	end

	local defaults = {}
	-- defaults["KEY_WRAP_MODE"] = 1
	defaults["KEY_FONT_SIZE"] = self._ui.Text:getFontSize()
	defaults["KEY_VERTICAL_SPACE"] = 3
	if app:isInnerTencent() then
		defaults.KEY_FONT_FACE = Res.font
	end

	local richText = ccui.RichText:createWithXML(msg:getRoomTextXml(), defaults)
	if nil == richText then
		self._bg:setContentSize(self._bgSize)
		self:setContentSize(self._initSize)
		return
	end
	richText:setAnchorPoint(cc.p(0, 0))
	richText:ignoreContentAdaptWithSize(false)
	richText:setOpenUrlHandler(handler(self, self.onClick))
	self._bg:addChild(richText, 2)
	self._textRT = richText
	richText:setPosition(20, 10)

	richText:setContentSize(cc.size(self._maxWid - 30, 0))
	richText:formatText()
	local t_size = richText:getFormatSize()
	richText:setContentSize(t_size)

	if msg:isAudioMsg() then
		local btnNode = richText:getChildByTag(100)
		if btnNode then
			self._voiceBtn = IMVoiceBtn.create(btnNode, msg)
			self._voiceBtn:onBtnClick(handler(self, self.onVoiceBtnClick))
		end
	end
	
	local bgHeight = math.max(t_size.height + 47, self._bgSize.height)
	local bgWidth = math.max(t_size.width + 30, self._bgSize.width)

	-- dump(t_size, "t_size")
	-- print("bgWidth", bgWidth)
	-- print("bgHeight", bgHeight)
	
	local newContSize = cc.size(self._initSize.width, self._initSize.height + bgHeight - self._bgSize.height)
	self:setContentSize(newContSize)
	self._ui.Panel:setContentSize(newContSize)
	-- dump(newContSize, "newContSize")

	self._bg:setContentSize(cc.size(bgWidth, bgHeight))
	self._bg:getChildByName("bg"):setContentSize(cc.size(bgWidth, bgHeight))

	ccui.Helper:doLayout(self)
	self._IMPriRender:setPositionY(newContSize.height - 80)
end

function ClassRef:onClickLink(cb)
	self._onClickLink = cb
end

function ClassRef:playVoiceAction()
	if self._voiceBtn and self._voiceBtn:isVisible() then
		self._voiceBtn:play()
	end
end

function ClassRef:stopVoiceAction()
	if self._voiceBtn and self._voiceBtn:isVisible() then
		self._voiceBtn:stopPlay()
	end
end

function ClassRef:onVoiceBtnClick()
	if self._data.downloadState == im.MessageAttachState.Downloaded then
		url = "audio|play"
	elseif self._data.downloadState == im.MessageAttachState.NeedDownload then
		url = "audio|download"
	end
	executeFunc(self._onClickLink, url, self._data, self)
end

function ClassRef:onClick(sender, url)
	if not self:isVisible() then return end
	-- print("click url = " .. url)
	if string.len(url) == 0 then return end

	if url ~= "audio|play" then
		AM.playClickItemSound()
	end

	local pos
	if string.isStartWith(url, "p|") or string.isStartWith(url, "u|") then
		local size = sender:getContentSize()
		pos = sender:convertToWorldSpace(cc.p(size.width, size.height/2))
	end
	executeFunc(self._onClickLink, url, self._data, pos)
end

return ClassRef